//
//  WBHomeViewController.swift
//  新浪微博
//
//  Created by ZTE on 2017/12/1.
//  Copyright © 2017年 ZTE. All rights reserved.
//

import UIKit

//定义全局常量 尽量使用 private修饰 否则到处都可以访问
private let cellID = "cellID"

class WBHomeViewController: WBBaseViewController {

    //微博数据数组
    private lazy var statusList = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
    }

    //加载数据
    override func loadData() {
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
            
            if !self.isPullup {
                self.statusList.removeAll()
            }
            
            for i in 0..<20 {
                
                if self.isPullup {
                    //将数据追加到底部
                    self.statusList.append("上拉 \(i)")
                    
                }else {
                    
                    
                    //将数据插入到数组的顶部
                    self.statusList.insert(i.description, at: 0)
                }
                
                
            }
            
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            
            //恢复上拉刷新标记
            self.isPullup = false
            
            self.tableView?.reloadData()
        }
        
        
    }
    
    
    //显示好友
    @objc private func showFriends() {
        
      
        let friendsVC = WBFriendsViewController()
        
        navigationController?.pushViewController(friendsVC, animated: true)
        
    }
    
    
    //重写父类的方法
    override func setUpUI() {
        
        super.setUpUI()
       // view.backgroundColor = UIColor.white
        //设置导航栏按钮
        //swift 调用 OC 返回 instancetye 的方法 判断不出是否可选        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: 16, target: self, action: #selector(showFriends), isBack: false)
   
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
    }
}

//MARK: - 表格数据源方法
extension WBHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        //1.取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
        
        //2.设置cell
        cell.textLabel?.text = statusList[indexPath.row]
        //3.返回cell
        return cell
    }
    
}

